--[[
MelonScript
Chat.
--]]


// Lots of credits to chewgum.
CHAT = { }

Commands = 
{
	setname = "ccm_setname",
}

function TalkIC( pos, range )
	local plys = {}
	
	for k, v in pairs( player.GetAll() ) do
		if ( v:GetPos():Distance( pos ) < range ) then
			table.insert( plys, v )
		end
	end
	
	return plys
end

function GM:PlayerSay( ply, text, public )
	self.BaseClass:PlayerSay( ply, text )
	
	if ( string.find( text, "#" ) == 1 ) then
		local command = string.sub( string.Explode( " ", text )[ 1 ], 2 )
		if Commands[ command ] then
			ply:ConCommand( Commands[ command ] .. string.sub( text, string.len( command ) + 2 ) )
			return ""
		else
			ply:PrintChat( "Invalid Command", false )
			return ""
		end
	end
	
	local exp = string.Explode( " ", text )
	if ( CHAT[ exp[ 1 ] ] ) then
		local len = string.len( text )
		local sub = string.len( exp[ 1 ] ) + 2
		CHAT[ exp[ 1 ] ]( ply, exp, string.sub( text, sub, len ) )
		return ""
	else
		for _, pl in pairs( player.GetAll() ) do
			if ( ply:GetPos():Distance( pl:GetPos() ) < 150 ) then
				pl:SendChatText( Color( 32, 178, 170, 255 ), ply:Nick() .. " says ", Color( 255, 255, 255, 255 ), "'" .. ply:CheckGrammar( text ) .. "'")
			end
		end
	end
	
	return ""
end

CHAT[ "/ooc" ] = function( ply, args, text )
for k, v in pairs( player.GetAll() ) do
if ( ply:IsAdmin() or ply:IsSuperAdmin() ) then
			v:SendChatText( Color( 20, 255, 20, 255 ),"[ADMIN-OOC] ", Color( 32, 178, 170, 255 ), ply:Nick(), Color( 255, 255, 255, 255 ), ": ", Color( 255, 255, 255, 255 ), text )
else
			v:SendChatText( Color( 20, 255, 20, 255 ), "[OOC] ", Color( 32, 178, 170, 255 ), ply:Nick(), Color( 255, 255, 255, 255 ), ": ", Color( 255, 255, 225, 255 ), text )
	end
end
end

CHAT[ "/radio" ] = function( ply, args, text )
for k, v in pairs( player.GetAll() ) do
if ( v:Team() == ply:Team() ) then
		v:SendChatText( Color( 20, 255, 20, 255 ), "[Radio] ", Color( 32, 178, 170, 255 ), ply:Nick(), Color( 255, 255, 255, 255 ), " radios in ", Color( 255, 255, 225, 255 ), "'" .. ply:CheckGrammar( text ) .. "'" )
end
end
end

CHAT[ "/whisper" ] = function( ply, args, text )
	for k, v in pairs( TalkIC( ply:GetPos(), 80 ) ) do
		v:SendChatText( Color( 127, 255, 0, 255 ), "[Whisper] ", Color( 32, 178, 170, 255 ), ply:Nick() .. " whispers ", Color( 255, 255, 225, 255 ), "'" .. ply:CheckGrammar( text ) .. "'" )
	end
end

CHAT[ "/yell" ] = function( ply, args, text )
	for k, v in pairs( TalkIC( ply:GetPos(), 450 ) ) do
		v:SendChatText( Color( 127, 255, 0, 255 ), "[Yell] ", Color( 32, 178, 170, 255 ), ply:Nick() .. " yells ", Color( 255, 255, 225, 255 ), "'" .. ply:CheckGrammar( text ) .. "'" )
	end
end

CHAT[ "/looc" ] = function( ply, args, text )
	for k, v in pairs( TalkIC( ply:GetPos(), 200 ) ) do
		v:SendChatText( Color( 127, 255, 0, 255 ), "[LOOC] ", Color( 32, 178, 170, 255 ), ply:Nick() .. ": ", Color( 255, 255, 225, 255 ), ply:CheckGrammar( text ) )
	end
end

CHAT[ "/me" ] = function( ply, args, text )
	for k, v in pairs( TalkIC( ply:GetPos(), 150 ) ) do
		v:SendChatText( Color( 127, 255, 0, 255 ), "** ", Color( 32, 178, 170, 255 ), ply:Nick() .. " ", Color( 255, 255, 225, 255 ), string.lower( ply:CheckGrammar( text ) ) )
	end
end

CHAT[ "/action" ] = function( ply, args, text )
	for k, v in pairs( TalkIC( ply:GetPos(), 150 ) ) do
		v:SendChatText( Color( 127, 255, 0, 255 ), "** ", Color( 255, 255, 225, 255 ), string.lower( ply:CheckGrammar( text ) ) )
	end
end

CHAT[ "/advert" ] = function( ply, args, text )

for k, v in pairs( player.GetAll() ) do
if ( v:IsAdmin() ) then
	v:PrintConsole( ply:Nick() .. " made a advert.", true )
end
v:SendChatText( Color( 127, 255, 0, 255 ), "[Advert] ", Color( 151, 211, 255, 255 ), ply:CheckGrammar( text ) )
end
end

CHAT[ "/broadcast" ] = function( ply, args, text )
		for k, v in pairs( player.GetAll() ) do
			v:SendChatText( Color( 127, 255, 0, 255 ), "[BROADCAST]: ", Color( 255, 20, 20, 255 ), ply:CheckGrammar( text ) )
		end
end
